package com.wanhe.viewconf.views.base.text;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

import com.wanhe.base.util.Logger;

/**
 * 需要根据文字显示宽度和高度来自动确定文字的大小，无需设置文字大小。
 * Created by Administrator on 2016/5/27.
 */
public class AutoTextView extends TextView {
    private static final String TAG = AutoTextView.class.getSimpleName();

    public AutoTextView(Context context) {
        this(context, null);
    }

    public AutoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heigth = MeasureSpec.getSize(heightMeasureSpec);
        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {

            //1，计算文本的高度
            Paint paint = getPaint();
            Paint.FontMetricsInt fm = paint.getFontMetricsInt();
            float textHeight = Math.abs(fm.bottom) + Math.abs(fm.top);

            //2，计算比例
            float scale = heigth / textHeight;

            //3，算出新的字体大小
            float newTextSize = paint.getTextSize() * scale;
            paint.setTextSize(newTextSize);

            //4，最后计算文字的宽度
            int width = (int) paint.measureText(getText().toString());

            Logger.i(TAG, "=========================onMeasure================================");
            Logger.i(TAG, "width:  " + width);
            int newWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width + 10, MeasureSpec.EXACTLY);
            super.onMeasure(newWidthMeasureSpec, newWidthMeasureSpec);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
        Logger.i(TAG, "====================================onMeasure ---ok===========================================");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect targetRect = new Rect();
        canvas.getClipBounds(targetRect);

        Paint paint = new Paint(getPaint());

        int color = getCurrentTextColor();
        paint.setColor(color);

        float top = targetRect.top;
        float bottom = targetRect.bottom;
        float left = targetRect.left;
        float right = targetRect.right;

        float width = targetRect.width();
        float heigth = targetRect.height();

        Logger.i(TAG, "===============================onDraw Rect 数值打印====================================");
        Logger.i(TAG, "width: " + width + "  heigth: " + heigth);
        Logger.i(TAG, "top: " + top + "  bottom: " + bottom);
        Logger.i(TAG, "left: " + left + "  right: " + right);

        /**
         * 需要根据文字现有的高度还有宽度和控件的高度和宽度进行对比，得到的比例就是文字大小的缩放比例
         */

        Paint.FontMetricsInt fm1 = paint.getFontMetricsInt();
        int textHeight = Math.abs(fm1.bottom) + Math.abs(fm1.top);
        float textWidth = paint.measureText(getText().toString());

        Rect textBound = new Rect();
        paint.getTextBounds(getText().toString(), 0, getText().toString().length(), textBound);
        int boundLeft = textBound.left;
        int boundTop = textBound.top;
        int boundRigth = textBound.right;
        int boundBottom = textBound.bottom;

        int boundWidth = textBound.width();
        int boundHeight = textBound.height();

        float x = (width - textWidth) / 2;
        float y = (heigth - boundHeight) / 2;


        Logger.i(TAG, "====================================onDraw 文本的宽高 数值打印===========================================");
        Logger.i(TAG, "textWidth: " + textWidth + "  textHeight: " + textHeight);
        Logger.i(TAG, "左边距离: " + x + "  顶部距离: " + y);


        Logger.i(TAG, "====================================onDraw 文本的Bound数据===========================================");
        Logger.i(TAG, "boundLeft: " + boundLeft + "  boundTop: " + boundTop);
        Logger.i(TAG, "boundRigth: " + boundRigth + "  boundBottom: " + boundBottom);
        Logger.i(TAG, "boundWidth: " + boundWidth + "  boundHeight: " + boundHeight);


        left = left + x;
        top = top + y + boundHeight;

        canvas.drawText(getText().toString(), left, top, paint);
        Logger.i(TAG, "====================================onDraw ---ok===========================================");
    }
}
